package service

import (
	"lib/id"
	"lib/jwt"
	"lib/structs/tables"
	"lib/structs/vap"
	"time"
	"vap/dao"
)

var ShHostServ = new(shHostService)

type shHostService struct {
}

func (s shHostService) Add(in vap.AddShHost, u *jwt.CustomClaims) (err error) {
	m := tables.VapShHost{
		Id:       id.GetSnowId(),
		Uid:      in.Uid,
		HostName: in.HostName,
		Ip:       in.Ip,
		ConnPort: in.ConnPort,
		AuthType: in.AuthType,
		Username: in.Username,
		Password: in.Password,
		Remark:   in.Remark,
		AutoCreateInfo: tables.AutoCreateInfo{
			Creator:   u.Nickname,
			CreatorId: u.UserID,
			CreatedAt: time.Now().Unix(),
			UpdatedAt: 0,
			DeletedAt: 0,
		},
	}
	err = dao.ShHostDao.Create(&m)
	return
}

func (s shHostService) Delete(id int64, u *jwt.CustomClaims) (err error) {
	err = dao.ShHostDao.DeleteById(id)
	return
}

func (s shHostService) Page(in vap.SearchShHost) (li []tables.VapShHost, total int64, err error) {
	if in.PageNum < 1 {
		in.PageNum = 1
	}
	li, total, err = dao.ShHostDao.FindList(in)
	return
}
